package com.example.docking.controller;

import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.BillPushData;
import com.example.docking.dao.jindie.response.SaveData;
import com.example.docking.service.impl.DataServiceImpl;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.HashMap;

//调用单据下推将直接保存新建目标单据并返回单据id
public class BillPushController {
    private static final Logger logger = LoggerFactory.getLogger(SalesOrderController.class);

    public ResponseData billPush(String id, String source_bill, String target_bill) {
        ResponseData responseData = new ResponseData();
        DataServiceImpl dataService = new DataServiceImpl();
        BillPushData billPushData = new BillPushData();
        HashMap<String, String> map = new HashMap<String, String>();
        Gson gson = new Gson();
        billPushData.setId(id);
        billPushData.setSource_bill(source_bill);
        billPushData.setTarget_bill(target_bill);
        ApiResult auditData = null;
        try {
            auditData = dataService.InsertDataPush(Constants.BILL_PUSH, map, billPushData);
            Type type1 = new TypeToken<ResponseData<SaveData>>() {
            }.getType();
            ResponseData<SaveData> response = gson.fromJson(auditData.getBody(), type1);
            if (response.getErrcode() == 0) {
                logger.info("推送成功Bill push success");

            } else {
                logger.info("推动失败Bill push failed");
            }
            logger.info("result:{}", response);
            responseData = response;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return responseData;
    }

    public static void main(String[] args) {
        BillPushController billPushController = new BillPushController();
        billPushController.billPush("2198732425306118144", "pur_bill_request", "pur_bill_order");
    }

}
